/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef MC_IMU_H
#define MC_IMU_H

#include <ipmb/ipmb.h>
#include <mc/common.h>
#include <mc/engine/service.h>
#include <memory>
#include <stdint.h>
#include <string>
#include <vector>

namespace dev {

typedef enum pci_info_address {
    VID_DID_ADDRESS   = 0x00, // PCI配置空间中的VID/DID信息起始地址
    SVID_SDID_ADDRESS = 0x2C, // PCI配置空间中的SVID/SDID信息起始地址
    BUS_NUMBER_ADDRESS =
        0x18, // PCI配置空间中的Primary/Secondary/Subordinate Bus Number信息起始地址
} pci_info_address_t;

typedef struct imu_pcie_device_req {
    uint8_t            system_id;
    uint8_t            socket_id;
    uint8_t            bus;
    uint8_t            device;
    uint8_t            function;
    pci_info_address_t pci_info_address;
    uint8_t            rlen;
} imu_pcie_device_req_t;

typedef struct imu_pcie_info {
    bool    is_local;
    uint8_t cpu_id;
    uint8_t address;
    uint8_t function_num;
    uint8_t device_num;
    uint8_t bus_num;
    uint8_t read_length;
} imu_pcie_info_t;

typedef struct pmu_version_info {
    uint8_t device1;
    uint8_t device2;
    uint8_t pmu_version1;
    uint8_t pmu_version2;
} pmu_version_info_t;

class MC_API imu {
public:
    explicit imu(mc::engine::service* service);
    ~imu();

    std::optional<std::vector<uint8_t>> get_info_from_imu(bool                   is_local,
                                                          imu_pcie_device_req_t& pcie_device_info);

    bool get_pmu_status(pmu_version_info& version_info);

    bool send_ipmi_request(uint8_t* reqdata, uint32_t reqdata_len, std::string& rspdata);

    std::optional<std::vector<uint8_t>> send_pmu_pci_request(const imu_pcie_info& pcie_info);

    std::vector<uint8_t> parse_pmu_pci_response(const std::vector<uint8_t>& response_data,
                                                uint8_t                     read_length);

private:
    std::unique_ptr<mc::ipmb> m_ipmb;
    mc::engine::service*      m_service;
};

} // namespace dev

#endif // MC_IMU_H